
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;

namespace Tathata.Web.Administration
{
    public partial class Administration_CountryDetails : BaseNopAdministrationPage
    {
        private void BindData()
        {
            Country country = CountryManager.GetByCountryID(this.CountryID);
            if (country != null)
            {
                this.txtName.Text = country.Name;
                this.cbAllowsRegistration.Checked = country.AllowsRegistration;
                this.cbAllowsBilling.Checked = country.AllowsBilling;
                this.cbAllowsShipping.Checked = country.AllowsShipping;
                this.txtTwoLetterISOCode.Text = country.TwoLetterISOCode;
                this.txtThreeLetterISOCode.Text = country.ThreeLetterISOCode;
                this.txtNumericISOCode.Value = country.NumericISOCode;
                this.cbPublished.Checked = country.Published;
                this.txtDisplayOrder.Value = country.DisplayOrder;
            }
            else
                Response.Redirect("Countries.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Country country = CountryManager.GetByCountryID(this.CountryID);

                    if (country != null)
                    {
                        country = CountryManager.UpdateCountry(country.CountryID,
                            txtName.Text, cbAllowsRegistration.Checked,
                            cbAllowsBilling.Checked, cbAllowsShipping.Checked,
                            txtTwoLetterISOCode.Text, txtThreeLetterISOCode.Text,
                            txtNumericISOCode.Value, cbPublished.Checked,
                           txtDisplayOrder.Value);
                        Response.Redirect("CountryDetails.aspx?CountryID=" + country.CountryID.ToString());
                    }
                    else
                        Response.Redirect("Countries.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                CountryManager.DeleteCountry(this.CountryID);
                Response.Redirect("Countries.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        public int CountryID
        {
            get
            {
                return CommonHelper.QueryStringInt("CountryID");
            }
        }
    }
}
